// 根据节点生成对应的 html 标签

import { MNode, TextNode } from "../mnodes";

export class HtmlGenerator {
  private _createTag (tagName: string, selfClosing: boolean = false): string {
    let buffer: string = `<${tagName}`;
    if (selfClosing) {
      buffer += '/';
    }
    buffer += '>';
    return buffer;
  }

  emph (buffer: string, mnode: MNode, close: boolean): string {
    if (close) {
      buffer += this._createTag('/em');
    } else {
      buffer += this._createTag('em');
    }
    return buffer;
  }

  paragraph (buffer: string, mnode: MNode, close: boolean): string {
    if (close) {
      buffer += this._createTag('/p');
    } else {
      buffer += this._createTag('p');
    }
    return buffer;
  }

  strong (buffer: string, mnode: MNode, close: boolean): string {
    if (close) {
      buffer += this._createTag('/strong');
    } else {
      buffer += this._createTag('strong');
    }
    return buffer;
  }

  text (buffer: string, mnode: TextNode, close: boolean): string {
    buffer += this._createTag('span') + mnode.text;
    buffer += this._createTag('/span');
    return buffer;
  }

  del (buffer: string, mnode: MNode, close: boolean): string {
    if (close) {
      buffer += this._createTag('/del');
    } else {
      buffer += this._createTag('del');
    }
    return buffer;
  }
}

export default HtmlGenerator;